from PyQt4 import QtGui, QtCore

class QDirectoryChooser(QtGui.QWidget):
	"""
	A QLineEdit with a QPushButton. When a clicked() signal is sent to the button a 
	ExistingDirectory dialog is opened and a fileNameChanged signal is emited."""
	
	def __init__(self, parent=None, initial_dir=None, title=None):
		super(QDirectoryChooser, self).__init__(parent)
		self.title = title if title != None else ""
		self.current_dir = initial_dir if initial_dir != None else ""
		self.button = QtGui.QPushButton("...")
		self.button.setMaximumSize(20, 20)
		
		self.edit = QtGui.QLineEdit()
		self.edit.setMinimumSize(300, 20)
		
		self.lay = QtGui.QHBoxLayout()
		self.lay.addWidget(self.edit)
		self.lay.addWidget(self.button)
		self.lay.setMargin(0)
		self.setLayout(self.lay)
		
		self.setFocusProxy(self.edit)
		
		self.connect(self.button, QtCore.SIGNAL("clicked()"), self.choose_directory)
	
	def choose_directory(self):
		"""Opens a ExistingDirectory dialog. """
		dir = QtGui.QFileDialog.getExistingDirectory(self, self.title, self.edit.text())
		if dir:
			self.current_dir = dir
			self.edit.setText(dir)
			self.emit(QtCore.SIGNAL("fileNameChanged(PyQt_PyObject)"), dir)


if __name__ == "__main__":
	
	def teste(d):
		print d

	import sys
	app = QtGui.QApplication(sys.argv)
	d = QtGui.QDialog()
	lay = QtGui.QHBoxLayout(d)
	dc = QDirectoryChooser(None, "c:\\")
	lay.addWidget(dc)
	d.setLayout(lay)
	d.connect(dc, QtCore.SIGNAL("fileNameChanged(PyQt_PyObject)"), teste)
	sys.exit(d.exec_())